<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class mis_komplain_penugasan extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('mis_komplain_penugasan_model', 'Penugasan');
		$this->load->model('mis_combo_model', 'Combo');
		$this->secure_session();
	}

	function id_auto(){
		$query1 = $this->db->query("SELECT id_penugasan FROM (SELECT * FROM mis_komplain_penugasan ORDER BY id_penugasan DESC) WHERE rownum <=1");
	 				if ($query1->row_array()) {
	 					$getquery1 = $query1->row_array();
	 					$rowcount1 = $getquery1['id_penugasan'];
	 					$autonum1 = $rowcount1 + 1;
	 				} else {
					$autonum1 = 1;
				}
		$data['id_penugasan_auto'] = $autonum1;
		echo json_encode($data);
	}
	
	function index(){
		$data['title'] = "Setup Komplain Penugasan";

		$query = $this->db->query("SELECT id_penugasan FROM (SELECT * FROM mis_komplain_penugasan ORDER BY id_penugasan DESC) WHERE rownum <=1");
	 				if ($query->row_array()) {
	 					$getquery = $query->row_array();
	 					$rowcount = $getquery['id_penugasan'];
	 					$autonum = $rowcount + 1;
	 				} else {
					$autonum = 1;
				}
		$data['id_penugasan_auto'] = $autonum;

		$data['Status']=$this->Penugasan->GetApp();
		$data['arr_customer']	= $this->Combo->ISPCustomer();		
		viewByTemplate('mis_komplain_penugasan/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->Penugasan->GetJSON();
	}

	function getdata2()
	{
		$this->secure_session();
		echo $this->Penugasan->GetJSON2();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation		
		if (post("id_penugasan") == '') {
			$error->Add("id_penugasan", "ID Penugasan", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM mis_komplain_penugasan WHERE id_penugasan = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("id_penugasan"))))."'")) > 0) {
			$error->Add("id_penugasan", "ID Penugasan", $tpl_duplicate);
		} else if (post("nama") == '') {
			$error->Add("nama", "Nama Petugas", $tpl_empty);
		} /*else if (post("tgl_selesai") == '') {
			$error->Add("tgl_selesai", "Tanggal selesai", $tpl_empty);
		}*/

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	=  array(
					'id_penugasan' => $this->input->post('id_penugasan'),
					'id_komplain' => $this->input->post('id_komplain'),
					'kode_komplain' => $this->input->post('kode_komplain'),
					'tgl_komplain' => $this->input->post('tgl_komplain'),
					'nama' => $this->input->post('nama'),
					'komplain' => $this->input->post('komplain'),
					'nama_unit' => $this->input->post('nama_unit'),
					'status' => $this->input->post('status'),
					'keterangan' => $this->input->post('keterangan'),
					'tgl_selesai' => $this->input->post('tgl_selesai'),
					'id_departemen_pk' => $this->input->post('id_departemen_pk'),
					);

					$data['tgl_komplain']	= ($data['tgl_komplain'] == '' ? NULL : date_format3($data['tgl_komplain'], 'YYYY-MM-DD'));
					$data['tgl_selesai']	= ($data['tgl_selesai'] == '' ? NULL : date_format3($data['tgl_selesai'], 'YYYY-MM-DD'));
				
				if ($this->input->post('status') != null){
					$DataStatus = array('status' => $this->input->post('status'));
					$this->Penugasan->UpdateStatus($DataStatus, array('kode_komplain' => $this->input->post('kode_komplain')));
				}

				if (post("mode") == "create")
				{
					//$data['create_date']	= NOW;
					//$data['create_user']	= USERID;

					$this->Penugasan->Add($data);
					//$this->db->query=("UPDATE mis_komplain SET status = '".$this->input->post('status')."' WHERE a = '".$this->input->post('kode_komplain')."'");

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
					//$data['modify_date']	= NOW;
					//$data['modify_user']	= USERID;
					
					$this->Penugasan->Update($data, 
										array('id_penugasan'	=> $data['id_penugasan']));
					//$this->db->query=("UPDATE mis_komplain SET status ='".$this->input->post('status')."' WHERE a ='".$this->input->post('kode_komplain')."'");

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS002", "edit/" + (data.role_id ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result		= "";
		$pesan 			= "Data Terhapus";
		$id_penugasan 	= get('id_penugasan');
		
		if ($id_penugasan != "")
		{
			// Delete Single
			$arr_rule	= array('id_penugasan' => $id_penugasan);
			$ls_result = $this->Penugasan->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_penugasan' => $row['id_penugasan']);
				$ls_result .= $this->Penugasan->DeleteSingle($arr_rule);
			}
		}
		
		echo $pesan;
		echo "
			<script>
				loadPage(gs_path + '/' + controller + '/index');
			</script>
		";
	}
}
?>